﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public partial class frmDangKyMuonSach : DevExpress.XtraEditors.XtraForm
    {
        private Account acc;
        RequestForm rq;
        private List<BookRequest> lBookRequest;
        List<BookRequest> lBookNotBorrow = new List<BookRequest>();
        private frmTrangChu frmTrangChu;
        public frmDangKyMuonSach()
        {
            InitializeComponent();
        }

        public frmDangKyMuonSach(List<BookRequest> lBookRequest, Account acc)
        {
            // TODO: Complete member initialization
            this.lBookRequest = lBookRequest;
            this.acc = acc;
            InitializeComponent();
        }

        public frmDangKyMuonSach(List<BookRequest> lBookRequest, Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.lBookRequest = lBookRequest;
            this.acc = acc;
            InitializeComponent();
            this.frmTrangChu = frmTrangChu;
        }

        public frmDangKyMuonSach(Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.acc = acc;
            this.frmTrangChu = frmTrangChu;
            InitializeComponent();
        }


        private void frmDangKyMuonSach_Load(object sender, EventArgs e)
        {
            Member mem = DAO_Member.SelectMember(acc);
            nameLabel1.Text = mem.Name;
            phoneNumberLabel1.Text = mem.PhoneNumber;
            emailLabel1.Text = mem.Email;
            if (DAO_RequestForm.isExsitForm(mem.Email) == true)
            {
                rq = DAO_RequestForm.SelectRFByEmail(mem.Email);
                if (lBookRequest == null)
                    lBookRequest = rq.lBookRequest;
            }
            else
                if (lBookRequest == null)
                    lBookRequest = new List<BookRequest>();
            lBookRequestDataGridView.DataSource = new List<BookRequest>();
            for (int i = 0; i < lBookRequest.Count; ++i)
                if (lBookRequest[i].IsBorrow == false)
                    lBookNotBorrow.Add(lBookRequest[i]);
            lBookRequestDataGridView.DataSource = lBookNotBorrow;
            QLTV_System.SetNumberColumn(lBookRequestDataGridView);
            btnAddPhieuMuon.Enabled = true;
        }

        private void SetNumberColumn(DataGridView gridView)
        {
            for (int i = 0; i < gridView.RowCount; ++i)
                gridView.Rows[i].Cells[0].Value = (i + 1).ToString(); ;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string tabTittle = "Tra cứu sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            for (int i = 0; i < frmTrangChu.xtraTabControl1.TabPages.Count; i++)
            {
                if (frmTrangChu.xtraTabControl1.TabPages[i].Text == tabTittle)
                {
                    frmTrangChu.xtraTabControl1.TabPages.Remove(frmTrangChu.xtraTabControl1.TabPages[i]);
                    break;
                }
            }
            frmTrangChu.xtraTabControl1.TabPages.Add(tab);
            var hien = new frmTraCuuSach(lBookNotBorrow, acc, frmTrangChu);
            hien.TopLevel = false;
            hien.Dock = DockStyle.Fill;
            tab.Controls.Add(hien);
            hien.Show();
            frmTrangChu.xtraTabControl1.SelectedTabPageIndex = frmTrangChu.xtraTabControl1.TabPages.Count - 1;
            //var frm = new frmTraCuuSach(lBookNotBorrow,acc);
            //frm.Show();
            //this.Hide();           
        }

        private void btnAddPhieuMuon_Click(object sender, EventArgs e)
        {
            if (lBookNotBorrow.Count > 3)
                MessageBox.Show("Số lượng sách của bạn đăng ký chờ quá số lượng...");
            else
            {
                List<Book> lBook = new List<Book>();
                for (int i = 0; i < lBookNotBorrow.Count; ++i)
                {
                    lBook.Add(lBookNotBorrow[i].bookRequest);
                }
                bool kq = DAO_RequestForm.AddBookRequest(emailLabel1.Text, lBook);
                if (kq == true)
                {
                    MessageBox.Show("Đăng ký mượn sách thành công");
                    btnAddPhieuMuon.Enabled = false;
                }
            }
        }
    }
}